<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="1000">
    <div style="height: 500px">
      <JmReport v-if="reportParams.assetId" :chartId="chartId" :params="reportParams" />
    </div>
    <BasicForm @register="registerForm" />
    <template #footer v-if="showFooter">
      <a-button key="back" @click="closeModal">{{ common('closeText') }}</a-button>
      <a-button key="submit" type="error" style="margin-left: 12px" @click="handleSubmit('4')">{{ common('reject') }}</a-button>
      <a-button key="submit" type="primary" style="margin-left: 12px" @click="handleSubmit('3')">{{ common('pass') }}</a-button>
    </template>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from '../AssetApproval.data';
  import { saveOrUpdate } from '../AssetApproval.api';
  import { useI18n } from '/@/hooks/web/useI18n';
  import JmReport from '/@/components/JmReport/index.vue';
  import { assetUseQueryById } from '/@/views/asset/asset-use/api';
  import { assetBorrowingQueryById } from '/@/views/asset/asset-borrowing/api';
  import { assetRetirementQueryById } from '/@/views/asset/asset-disposal/api';

  import { assetReturnInventoryQueryById } from '/@/views/asset/asset-return-inventory/api';
  import { assetReturnResignationQueryById } from '/@/views/asset/asset-return-resignation/api';
  import { assetReturnQueryById } from '/@/views/asset/asset-return/api';
  import { assetTransferQueryById } from '/@/views/asset/asset-transfer/api';
  import { assetAllocationQueryById } from '/@/views/asset/asset-allocation/api';

  // const { t } = useI18n('asset.assetApproval');
  const { t: common } = useI18n('common');
  const fromData = ref<any>({});
  const chartId = ref('');
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 150,
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 24 },
  });
  const reportParams = ref({ id: '', assetId: '' });
  const showFooter = ref(true);
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单

    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      //表单赋值
      console.log(data.record);
      await setFieldsValue({
        ...data.record,
      });
      fromData.value = data.record;
      switch (fromData.value.type) {
        case '1':
          chartId.value = '889670840425824256';
          assetUseQueryById({ id: fromData.value.billid }).then((res) => {
            reportParams.value.id = fromData.value.billid;
            reportParams.value.assetId = res.assetList;
          });
          break;
        case '2':
          chartId.value = '892945105229103104';
          assetBorrowingQueryById({ id: fromData.value.billid }).then((res) => {
            reportParams.value.id = fromData.value.billid;
            reportParams.value.assetId = res.assetList;
          });
          break;
        case '3':
          chartId.value = '907490758111584256';
          assetRetirementQueryById({ id: fromData.value.billid }).then((res) => {
            reportParams.value.id = fromData.value.billid;
            reportParams.value.assetId = res.assetList;
          });
          break;
        case '5':
          chartId.value = '907445041926037504';
          assetTransferQueryById({ id: fromData.value.billid }).then((res) => {
            reportParams.value.id = fromData.value.billid;
            reportParams.value.assetId = res.assetList;
          });
          break;

        case '6':
          chartId.value = '908594207897214976';
          assetAllocationQueryById({ id: fromData.value.billid }).then((res) => {
            reportParams.value.id = fromData.value.billid;
            reportParams.value.assetId = res.assetList;
          });
          break;
        case '7':
          chartId.value = '907886509656432640';
          assetReturnInventoryQueryById({ id: fromData.value.billid }).then((res) => {
            reportParams.value.id = fromData.value.billid;
            reportParams.value.assetId = res.assetList;
          });
          break;

        case '8':
          chartId.value = '907886557475692544';
          assetReturnResignationQueryById({ id: fromData.value.billid }).then((res) => {
            reportParams.value.id = fromData.value.billid;
            reportParams.value.assetId = res.assetList;
          });
          break;
        case '9':
          chartId.value = '892679023201271808';
          assetReturnQueryById({ id: fromData.value.billid }).then((res) => {
            reportParams.value.id = fromData.value.billid;
            reportParams.value.assetId = res.assetList;
          });
          break;
      }
    }
    showFooter.value = data?.showFooter;
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !data?.showFooter });
  });
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? '新增' : common('audit')));

  //表单提交事件
  async function handleSubmit (status) {
    try {
      let values = await validate();
      console.log(values, fromData.value, 'testtesttest');
      setModalProps({ confirmLoading: true });
      //提交表单
      values.status = status;
      await saveOrUpdate({ ...fromData.value, ...values }, isUpdate.value);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
</script>

<style lang="less" scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-calendar-picker) {
    width: 100%;
  }
</style>
